package dao.imp;

import dao.It_userDAOplus;
import domain.Druiddb;
import domain.JDBCUntis;
import domain.t_user;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class t_userplus implements It_userDAOplus {
  static Connection conn = null;
  Statement st = null;
  PreparedStatement pst = null;
  ResultSet rs = null;

  static {
    conn = JDBCUntis.getconnection();
  }

  @Override
  public int insert(t_user user) {
    try{
    String sql="insert into t_user(name,sex) values(?,?)";
      pst=conn.prepareStatement(sql);
    pst.setObject(1,user.getName());
    pst.setObject(2,user.getSex());

pst.executeUpdate();
    }catch (Exception e ){
      e.printStackTrace();
    }finally{
      JDBCUntis.closet(conn,pst,null);
    }
    return 0;
  }

  @Override
  public int delect(int id) {
    String sql="delete from t_user where id="+id;
    try{
      Druiddb.reSetUrl("Druiddb.properties");
      conn=Druiddb.ds.getConnection();
              pst=conn.prepareStatement(sql);
pst.executeUpdate();

    }catch (Exception e ){
      e.printStackTrace();
    }finally{
      JDBCUntis.closet(conn,pst,null);
    }
    return 0;
  }

  @Override
  public int update(t_user user, int i) {
    try {
conn.setAutoCommit(false);
      String sql = "update t_user set name=? , sex=? where id=" + i;
      pst = conn.prepareStatement(sql);
      pst.setObject(1, user.getName());
      pst.setObject(2, user.getSex());
      pst.executeUpdate();
      conn.commit();
    } catch (Exception e) {
      e.printStackTrace();
      try {
        conn.rollback();
      } catch (SQLException throwables) {
        throwables.printStackTrace();
      }
    } finally {
      JDBCUntis.closet(conn, pst, null);
    }
    return 0;
  }

  @Override
  public int selectAll() {
    try {
      String sql = "select *  from t_user";
      pst = conn.prepareStatement(sql);
      rs = pst.executeQuery();
      while (rs.next()) {
        System.out.println(
            rs.getObject(1) + "  " + rs.getObject(2) + "  " + rs.getObject(3) + "  \n");
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUntis.closet(conn, pst, rs);
    }
    return 0;
  }

  // 根据条件查询
  public <T> List<T> Listselect(String sql, Class<T> cl, Object... name) {
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    List<T> list = new ArrayList();
    try {
      conn = JDBCUntis.getconnection();
      pst = conn.prepareStatement(sql);
      for (int i = 0; i < name.length; i++) {
        pst.setObject(i + 1, name[i]);
      }
      rs = pst.executeQuery();
      T t = cl.newInstance();
      while (rs.next()) {
        BeanInfo bf = Introspector.getBeanInfo(cl);
        PropertyDescriptor[] p = bf.getPropertyDescriptors();
        for (PropertyDescriptor pp : p) {
          if (pp.getName().equals("class")) {
            continue;
          }
          String na = pp.getName();
          Object value = rs.getObject(na);
          Method wr = pp.getWriteMethod();
          wr.invoke(t, value);
        }

        list.add(t);
        t = cl.newInstance();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally{
      JDBCUntis.closet(conn,pst,rs);
    }
    return list;
  }
}
